<?php
/*
 * template/popin.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
use RHX\Model\Object\XRules;
?>
<?php
$id	 = $this->id;
$codes	 = $this->codes;
$codec	 = $this->code;
$entree	 = $this->entree;
$sortie	 = $this->sortie;
$pause	 = $this->pause;
$repas	 = $this->repas;
$site	 = $this->site;
$lundi	 = $this->lundi;
$prefix	 = $this->prefix;
// Objets Temporelles
$Entree	 = new RHDateTime();
$Entree->setTime($entree);
//var_dump($Entree->getH());
//var_dump($Entree->getM());
$Sortie	 = new RHDateTime();
$Sortie->setTime($sortie);
//var_dump($Sortie->getH());
//var_dump($Sortie->getM());
?>
<div class="popin-arrow">
    <div class="popin-content">
	<div class="popin-title content-head">
	    <h4>Edition du Planning Semaine</h4>
	</div>
	<div class="popin-code">
	    <h5 style='width: 99%'>Codes d'activité</h5>
	    <div class='popin-code-content'>
		<?php foreach ($codes as $code): ?>
		    <?php $selected = ((int) $codec == (int) $code->id) ? 'selected' : ''; ?>
    		<div class='code <?php echo $selected; ?>' id="code-<?php echo $code->id; ?>">
    		    <div class='code-couleur' style='background: <?php echo $code->couleur; ?>'>
    			<a href='#' id="<?php echo $code->id; ?>" class="code-link" title='<?php echo $code->libelle; ?>' onclick="return changeCode(<?php echo $code->id; ?>);">
				<?php echo $code->code; ?>
    			</a>
    		    </div>
    		</div>
		<?php endforeach; ?>
	    </div>
	</div>
	<div class='popin-other'>
	    Heure Entrée<br>
	    <select id='entree_heure' name='entree_heure'>
		<?php for ($i = 0; $i < 24; $i++): ?>
		    <?php $value		 = ($i < 10) ? '0' . $i : $i; ?>
		    <?php $selected	 = ((int) $Entree->getH() == (int) $value) ? 'selected="selected"' : ''; ?>
    		<option value='<?php echo $value; ?>' <?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select> :
	    <select id='entree_minute' name='entree_minute'>
		<?php for ($i = 0; $i < 60; $i+= 5): ?>
		    <?php $value		 = ($i < 10) ? '0' . $i : $i; ?>
		    <?php $selected	 = ((int) $Entree->getM() == (int) $value) ? 'selected="selected"' : ''; ?>
    		<option value='<?php echo $value; ?>' <?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select>
	    <br>
	    Heure Sortie<br>
	    <select id='sortie_heure' name='entree_heure'>
		<?php for ($i = 0; $i < 24; $i++): ?>
		    <?php $value		 = ($i < 10) ? '0' . $i : $i; ?>
		    <?php $selected	 = ((int) $Sortie->getH() == (int) $value) ? 'selected="selected"' : ''; ?>
    		<option value='<?php echo $value; ?>' <?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select> :
	    <select id='sortie_minute' name='entree_minute'>
		<?php for ($i = 0; $i < 60; $i+= 5): ?>
		    <?php $value		 = ($i < 10) ? '0' . $i : $i; ?>
		    <?php $selected	 = ((int) $Sortie->getM() == (int) $value) ? 'selected="selected"' : ''; ?>
    		<option value='<?php echo $value; ?>' <?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select>
	    <hr>
	    <label>
		Pause : <input type="checkbox" id="pause" name="pause" <?php echo ((int) $pause == 1) ? 'checked="checked"' : '' ?> >
	    </label>
	    <label>
		Repas : <input type="checkbox" id="repas" name="repas" <?php echo ((int) $repas == 1) ? 'checked="checked"' : '' ?> >
	    </label>
	</div>
	<hr>
	<div class='popin-button' style='margin-bottom: 5px;'>
	    <button class='btn btn-success validate'>Valider</button>
	    <button class='btn btn-inverse' onclick='$(".rhPopover").remove();'>Fermer</button>
	</div>
	<input type="hidden" id="pl-id" name="id" value="<?php echo $id; ?>" />
	<input type="hidden" id="data-code" name="code" value="<?php echo $codec; ?>" />
	<input type="hidden" id="prefix" name="prefix" value="<?php echo $prefix; ?>" />
	<input type="hidden" id="paye" name="paye" value="" />
    </div>
</div>

<script type="text/javascript">
// Chargement
			    $(document).ready(function() {
				// Clic sur le bouton valider
				$('.validate').click(function(event) {
				    event.preventDefault();
				    // Variables
				    id = $('#pl-id').val();
				    code = $('#data-code').val();
				    entree = $('#entree_heure option:selected').val() + ':' + $("#entree_minute option:selected").val();
				    sortie = $('#sortie_heure option:selected').val() + ':' + $("#sortie_minute option:selected").val();
				    pause = ($("#pause").prop('checked')) ? 1 : 0;
				    repas = ($('#repas').prop('checked')) ? 1 : 0;
				    prefix = $('#prefix').val();
				    // Sauvegarde
				    ajax1 = $.ajax({
					type: 'post',
					url : '<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'save')) ?>/' + id,
					data: '&code=' + code + '&entree=' + entree + '&sortie=' + sortie + '&pause=' + pause + '&repas=' + repas + '&prefix=' + prefix
				    });
				    ajax1.done(function(data){
					// Affectation
					selector = '#' + matricule + '-' + prefix;
					//alert(selector);
					$(selector + '-code a').text(codec[code]);
					$(selector + '-entree a').text(entree);
					$(selector + '-sortie a').text(sortie);
					$(selector + '-pause a').text((pause === 1) ? 'X' : '');
					$(selector + '-repas a').text((repas === 1) ? '1' : '');
					// CSS
					$(selector + '-code').removeClass().addClass(codec[code]);
					$(selector + '-entree').removeClass().addClass(codec[code]);
					$(selector + '-sortie').removeClass().addClass(codec[code]);
					$(selector + '-pause').removeClass().addClass(codec[code]);
					$(selector + '-repas').removeClass().addClass(codec[code]);
					// Calcul des Cumuls
					calculateCumul(<?php echo $id; ?>, <?php echo $this->matricule; ?>);
					$(".rhPopover").remove();
				    });
				    ajax1.fail(function(){});
				});
				// Vérification des Heures
				$('#entree_heure, #entree_minute, #sortie_heure, #sortie_minute').change(function(){
				    // Vérification des horaires
				    verifyHour();
				});
			    });
			    function changeCode(newCode) {
				$('#data-code').val(newCode);
				$('.selected').removeClass('selected');
				$('#code-' + newCode).addClass('selected');

				// Vérification des horaires
				verifyHour();

				// Règle de Gestion
				switch (newCode) {
				    case <?php echo XRules::Present; ?>:
					// Présent
					Paye();
					break;
				    case <?php echo XRules::CongePaye; ?>:
					// Congé Payé
					withPause();
					noRepas();
					Paye();
					break;
				    case <?php echo XRules::NeTravaillePas; ?>:
					// OFF ne travaille pas
					withPause();
					Paye(false);
					break;
				    case <?php echo XRules::Indisponible; ?>:
					// IND : Indisponible
					RAZHour();
					noRepas();
					Paye(false);
					break;
				    case <?php echo XRules::Absent; ?>:
					// ABS
					noRepas();
					Paye(false);
					break;
				    case <?php echo XRules::CongeSansSolde; ?>:
					// CSS
					RAZHour();
					noRepas();
					Paye(false);
					break;
				    case <?php echo XRules::TransfertEntrant; ?>:
					// TE
					Paye();
					break;
				    case <?php echo XRules::TransfertSortant; ?>:
					// TS
					Paye();
					break;
				    case <?php echo XRules::ArretMaladie1; ?>:
					// AM1
					noRepas();
					Paye(false);
					break;
				    case <?php echo XRules::ArretMaladie2; ?>:
					// AM2
					noRepas();
					Paye();
					break;
				    case <?php echo XRules::AccidentTravail; ?>:
					// AT
					noRepas();
					Paye();
					break;
				    case <?php echo XRules::EcoleFormation; ?>:
					// EF
					Paye();
					break;
				}

				return false;
			    }
			    function RAZHour() {
				// Entree Heure à Zéro
				$('#entree_heure option:selected').removeAttr('selected');
				$('#entree_heure option').each(function() {
				    if ($(this).val() === '00') {
					$(this).attr('selected');
					return false;
				    }
				});
				// Entree Minute à zéro
				$('#entree_minute option:selected').removeAttr('selected');
				$('#entree_minute option').each(function() {
				    if ($(this).val() === '00') {
					$(this).attr('selected');
					return false;
				    }
				});
				// Sortie Heure à Zéro
				$('#sortie_heure option:selected').removeAttr('selected');
				$('#sortie_heure option').each(function() {
				    if ($(this).val() === '00') {
					$(this).attr('selected');
					return false;
				    }
				});
				// Sortie Minute à zéro
				$('#sortie_minute option:selected').removeAttr('selected');
				$('#sortie_minute option').each(function() {
				    if ($(this).val() === '00') {
					$(this).attr('selected');
					return false;
				    }
				});

				return false;
			    }
			    function noPause() {
				$("#pause").prop('checked', false);
			    }
			    function withPause(){
				$("#pause").prop('checked', true);
			    }
			    function noRepas() {
				$("#repas").prop('checked', false);
			    }
			    function withRepas(){
				$("#repas").prop('checked', true);
			    }
			    function noPauseAndRepas() {
				noPause();
				noRepas();
			    }
			    function withPauseAndRepas(){
				withPause();
				withRepas();
			    }
			    function Paye(p) {
				if (!p)
				    p = true;
				if (p === true) {
				    $('#paye').val(1);
				} else {
				    $('#paye').val(0);
				}
			    }
			    function verifyHour(){
				// Récupération des heures
				entree = $('#entree_heure option:selected').val() + ':' + $("#entree_minute option:selected").val();
				sortie = $('#sortie_heure option:selected').val() + ':' + $("#sortie_minute option:selected").val();
				// Traitement Ajax
				ajax = $.ajax({
				    dataType: 'json',
				    type: 'post',
				    //async: false,
				    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'planning', 'action'	 => 'verifyHour')) ?>',
				    data: '&entree=' + entree + '&sortie=' + sortie
				});
				ajax.success(function(data){
				    if(data.pause){
					withPause();
				    }
				    if(data.repas){
					withRepas();
				    }
				});
				ajax.error(function(){});
			    }
			    var codec = new Array();
<?php foreach ($codes as $c): ?>
            			    codec[<?php echo $c->id; ?>] = "<?php echo $c->code; ?>";
<?php endforeach; ?>
</script>
